﻿#include "ToolbarLens.h"
#include <QtDebug>
namespace Qly
{
ToolBarLens::ToolBarLens(const QString &title, QWidget *parent)
    :QToolBar(title, parent)
{
    m_old = 0;
    setupUi();

    QStringList a;
    a << "4X" << "10X" << "20X" << "40X";
    loadItem(a);

    connect(m_comboBoxLens, SIGNAL(currentIndexChanged(int )), this, SLOT(lensUpdate(int)));
    connect(m_actionSetup, SIGNAL(triggered()), this, SIGNAL(showSettingsDialog()));
    connect(m_actionClibLine, SIGNAL(triggered(bool)), this, SIGNAL(calibMode(bool)));
}

void ToolBarLens::setLensIndex(int n)
{
    //int old = m_comboBoxLens->currentIndex();
    m_comboBoxLens->setCurrentIndex(n);
}

void ToolBarLens::loadFromLensManager(LensManager * lensDataManager)
{
    int n = lensDataManager->lensCount();
    disconnect(m_comboBoxLens, SIGNAL(currentIndexChanged(int )), this, SLOT(lensUpdate(int)));
    m_comboBoxLens->clear();
    for(int i = 0; i < n; i++)
    {
        LensData& d = lensDataManager->lensByIndex(i);
        m_comboBoxLens->insertItem(i, d.displayName);
    }
    int c = lensDataManager->currentLensIndex();
    qDebug() << "loadLensData->currentLensIndex = " << c;
    m_comboBoxLens->setCurrentIndex(c);
    connect(m_comboBoxLens, SIGNAL(currentIndexChanged(int )), this, SLOT(lensUpdate(int)));
}

void ToolBarLens::setupUi()
{
    m_comboBoxLens = new QComboBox(this);
    m_comboBoxLens->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    m_comboBoxLens->setMinimumSize(100, 20);
    addWidget(m_comboBoxLens);
    addSeparator();

    m_actionClibLine = new QAction(this);
    QIcon icon1;
    icon1.addFile(QStringLiteral(":/NosepieceCommon/res/calibLine.png"), QSize(), QIcon::Normal, QIcon::Off);
    m_actionClibLine->setIcon(icon1);
    addAction(m_actionClibLine);
    m_actionClibLine->setCheckable(true);

    m_actionSetup = new QAction(this);
    QIcon icon2;
    icon2.addFile(QStringLiteral(":/NosepieceCommon/res/setup.png"), QSize(), QIcon::Normal, QIcon::Off);
    m_actionSetup->setIcon(icon2);
    addAction(m_actionSetup);

}

void ToolBarLens::lensUpdate(int newlens)
{
    qDebug() << "lensUpdate : newlens = " << newlens;
    emit changeLens(m_old, newlens);
    m_old = newlens;
}

void ToolBarLens::loadSettings(QSettings &settings)
{
   QStringList name;
   name << settings.value("LensName/LENS0", "4X").toString();
   name << settings.value("LensName/LENS1", "10X").toString();
   name << settings.value("LensName/LENS2", "40X").toString();
   name << settings.value("LensName/LENS3", "80X").toString();
//   name << settings.value("LensName/LENS4", "100X").toString();

   loadItem(name);
}

void ToolBarLens::loadItem(QStringList list)
{
    m_comboBoxLens->clear();
    m_comboBoxLens->addItems(list);
    m_comboBoxLens->setCurrentIndex(0);
    m_old = 0;

}

}//namespace Qly
